import { NModal, type ModalProps } from "naive-ui";
import type { BaseComponentType, OmitHTMLAttributes } from "../types";
import { getSlotsDom } from "../utils";

export type ModalFactoryConstructorType = {
  props?: ModalProps;
  attrs?: OmitHTMLAttributes<"draggable">;
  defaultSlot?: BaseComponentType;
  coverSlot?: BaseComponentType;
  headerSlot?: BaseComponentType;
  headerExtraSlot?: BaseComponentType;
  footSlot?: BaseComponentType;
  actionSlot?: BaseComponentType;
  iconSlot?: BaseComponentType;
  closeSlot?: BaseComponentType;
};

export class ModalFactory {
  private attrs: OmitHTMLAttributes<"draggable"> = {};
  private props: ModalProps = {};
  private defaultSlot: BaseComponentType = null;
  private coverSlot: BaseComponentType = null;
  private headerSlot: BaseComponentType = null;
  private headerExtraSlot: BaseComponentType = null;
  private footSlot: BaseComponentType = null;
  private actionSlot: BaseComponentType = null;
  private iconSlot: BaseComponentType = null;
  private closeSlot: BaseComponentType = null;

  constructor(param?: ModalFactoryConstructorType) {
    if (param?.props) this.setProps(param.props);
    if (param?.attrs) this.setAttrs(param.attrs);
    if (param?.defaultSlot) this.setDefault(param.defaultSlot);
    if (param?.coverSlot) this.setCover(param.coverSlot);
    if (param?.headerSlot) this.setHeader(param.headerSlot);
    if (param?.headerExtraSlot) this.setHeaderExtra(param.headerExtraSlot);
    if (param?.footSlot) this.setFoot(param.footSlot);
    if (param?.actionSlot) this.setAction(param.actionSlot);
    if (param?.iconSlot) this.setIcon(param.iconSlot);
    if (param?.closeSlot) this.setClose(param.closeSlot);
  }

  setProps(props: ModalProps) {
    this.props = props;
    return this;
  }

  setAttrs(attrs: OmitHTMLAttributes<"draggable">) {
    this.attrs = attrs;
    return this;
  }

  setDefault(component: BaseComponentType) {
    this.defaultSlot = component;
    return this;
  }

  setCover(component: BaseComponentType) {
    this.coverSlot = component;
    return this;
  }

  setHeader(component: BaseComponentType) {
    this.headerSlot = component;
    return this;
  }

  setHeaderExtra(component: BaseComponentType) {
    this.headerExtraSlot = component;
    return this;
  }

  setFoot(component: BaseComponentType) {
    this.footSlot = component;
    return this;
  }

  setAction(component: BaseComponentType) {
    this.actionSlot = component;
    return this;
  }

  setIcon(component: BaseComponentType) {
    this.iconSlot = component;
    return this;
  }

  setClose(component: BaseComponentType) {
    this.closeSlot = component;
    return this;
  }

  create() {
    if (this.props.preset === "card")
      return (
        <NModal {...this.attrs} {...this.props}>
          {{
            default: () => getSlotsDom(this.defaultSlot),
            cover: () => getSlotsDom(this.coverSlot),
            header: () => getSlotsDom(this.headerSlot),
            "header-extra": () => getSlotsDom(this.headerExtraSlot),
            foot: () => getSlotsDom(this.footSlot),
            action: () => getSlotsDom(this.actionSlot),
          }}
        </NModal>
      );
    else if (this.props.preset === "dialog")
      return (
        <NModal {...this.attrs} {...this.props}>
          {{
            default:
              this.defaultSlot != null
                ? () => getSlotsDom(this.defaultSlot)
                : null,
            header:
              this.headerSlot != null
                ? () => getSlotsDom(this.headerSlot)
                : null,
            icon:
              this.iconSlot != null ? () => getSlotsDom(this.iconSlot) : null,
            action:
              this.actionSlot != null
                ? () => getSlotsDom(this.actionSlot)
                : null,
            close:
              this.closeSlot != null ? () => getSlotsDom(this.closeSlot) : null,
          }}
        </NModal>
      );
    else
      return (
        <NModal {...this.attrs} {...this.props}>
          {{
            default: () => getSlotsDom(this.defaultSlot),
          }}
        </NModal>
      );
  }
}
